/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.util;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;
import java.io.File;
import java.io.IOException;
import java.awt.image.RenderedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

/**
 *
 * @author roniere
 */
public class ConverterImg {

    private static InputStream is;

    public static byte[] convertTiffToJpg(InputStream in) {
        ImageDecoder decoder = (ImageDecoder) ImageCodec.createImageDecoder("tiff", in, null);
        RenderedImage ri;
        ByteArrayOutputStream baos = null;
        try {
            ri = decoder.decodeAsRenderedImage();
            baos = new ByteArrayOutputStream();
            JPEGEncodeParam JPEGparam = new JPEGEncodeParam();
            JPEGparam.setQuality(Float.MAX_VALUE);
            ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg", baos, JPEGparam);
            encoder.encode(ri);
        } catch (IOException ex) {
            Logger.getLogger(ConverterImg.class.getName()).log(Level.SEVERE, null, ex);
        }

        return baos.toByteArray();
    }


    public static void convertTiffToJpg(String in, String out) throws Exception {
        ImageDecoder decoder = (ImageDecoder) ImageCodec.createImageDecoder("tiff", new File(in), null);
        RenderedImage ri = decoder.decodeAsRenderedImage();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JPEGEncodeParam JPEGparam = new JPEGEncodeParam();
        JPEGparam.setQuality(Float.MIN_VALUE);
        ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg", baos, JPEGparam);
        encoder.encode(ri);

        byte[] file = baos.toByteArray();
        FileOutputStream fileOuputStream =
                new FileOutputStream("/opt/2i.jpg");
        fileOuputStream.write(file);
        fileOuputStream.close();
    }

    public static void connect() throws MalformedURLException, SmbException, IOException {
        String user = "samanta";
        String pass = "almendra";

        String path = "smb://zeus/imagem/ESCRITURAS/E259/100/image0000120B.tif";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", user, pass);
        SmbFile smbFile = new SmbFile(path, auth);

        SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
        smbfos.write("testing....and writing to a file".getBytes());

        is = smbFile.getInputStream();

        int read = 0;
        byte[] bytes = new byte[1024];

        OutputStream outputStream =
                new FileOutputStream(new File("/opt/testebk.tiff"));

        while ((read = is.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        outputStream.close();

        System.out.println("completed ...nice !");
    }

    public static void backupMain() {
        File file = new File("/opt/ARQUIVO CARTORIO/ESCRITURAS/");

        for (File livros : file.listFiles()) {
            for (File num : livros.listFiles()) {
                if (num.isDirectory()) {
                    for (File arq : num.listFiles()) {
                        if(arq.getName().contains(".jpg") |
                                arq.getName().contains(".pdf") |
                                arq.getName().contains(".jpeg") |
                                arq.getName().contains(".tif")|
                                arq.getName().contains(".tiff"))
                        System.err.println("Livro: " + livros.getName() + " Nº: " + num.getName() + " Arquivo: " + arq.getName());
                    }
                }

            }
        }
    }

    public static void backup(File file, String livro) {
        if (file.isDirectory()) {
            for (File file1 : file.listFiles()) {
                backup(file1, livro);
            }
        } else {
            System.err.println(" Arquivo: " + file.getName());
        }
    }

    public static void backupRec(File file, String livro) {
        if (file.isDirectory()) {
            System.err.print(" nº: " + file.getName());
            for (File file1 : file.listFiles()) {
                backup(file1, livro);
            }
        } else {
            System.err.println(" Arquivo: " + file.getName());
        }
    }
}
